/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LineComponent.java
 *
 * Created on 2011-7-23, 18:41:12
 */
package me.zhy.swing;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import javax.swing.JFrame;
import me.zhy.math.LineSegment;
import me.zhy.math.MathUtilities;
import me.zhy.math.Point;

/**
 *
 * @author Moon
 */
public class LineSegmentComponent extends javax.swing.JComponent {

    Stroke dash;

    public LineSegmentComponent() {
        this(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND,
                3.5f, new float[]{15, 10,}, 0f));
    }

    /** Creates new form LineComponent */
    public LineSegmentComponent(Stroke dash) {
        this.dash = dash;
        initComponents();
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    private int x0;
    private int x1;
    private int y0;
    private int y1;

    public int getX0() {
        return x0;
    }

    public int getX1() {
        return x1;
    }

    public int getY0() {
        return y0;
    }

    public int getY1() {
        return y1;
    }

    public void setLocation(int x0, int y0, int x1, int y1) {
        this.x0 = x0;
        this.x1 = x1;
        this.y0 = y0;
        this.y1 = y1;
        this.setBounds(Math.min(x0, x1), Math.min(y0, y1), Math.abs(x0 - x1) + 1, Math.abs(y0 - y1) + 1);
    }

    @Override
    public void paint(Graphics grap) {
        ((Graphics2D) grap).setStroke(dash);
        int minx = Math.min(x0, x1);
        int miny = Math.min(y0, y1);
//        super.paint(grap);
        grap.setColor(getForeground());
        grap.setColor(Color.red);
        grap.drawLine(x0 - minx, y0 - miny, x1 - minx, y1 - miny);
    }

    public void setLocation0(int x0, int y0) {
        this.setLocation(x0, y0, x1, y1);
    }

    @Override
    public boolean contains(int x, int y) {
        int bx = this.getX();
        int by = this.getY();
        double dis = MathUtilities.distance(new Point(x + bx, y + by), new LineSegment(x0, y0, x1, y1));
        return dis < 5;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        LineSegmentComponent l = new LineSegmentComponent();
        l.setLocation(10, 10, 100, 100);
        l.setForeground(Color.red);
        frame.add(l);
    }

    public void setLocation1(int x1, int y1) {
        this.setLocation(x0, y0, x1, y1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void setDash(BasicStroke basicStroke) {
        this.dash = basicStroke;
    }
}
